import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";




/**
 * 搬运工行为。不计算数量
 * 把一个地方的资源搬运到另一个地方，不计种类不计数量(目前只搬运能量)
 */ 
 /*@profile*/
export class Porter<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        
        // 2.
      
        const creep = arg.entity;
     
        // 搬运工
     
       
        if(creep.store.getUsedCapacity()==0)
        {
            creep.memory.state = 0;

        }
        else  if(creep.store.getFreeCapacity()==0)
        {
            creep.memory.state = 1;
            // 绝不空载
        }
        if(creep.memory.state)
        {
            const getTransferObject=( )=>
            {
                const spawnentity = arg.entity.getEntity("SpawnEntity",creep.memory.OwnerRoom);
                // 运
                const containers = spawnentity.getContainer().map(e=>Game.getObjectById(e)) as StructureContainer[];
                if(containers.length>0)
                {
                    for(const e of containers)
                    {
                        if(e.store.getFreeCapacity()>0)
                        {
                            
                            return e;
                        }
                    }
                }
                
                {
                    // 没有container则直接给spawn
                    const spawn = spawnentity.spawnStructures();
                    for(const e of spawn)
                    {
                        if(e.store.getFreeCapacity(RESOURCE_ENERGY)>0)
                        {
                            
                            return e;
                        }
                        
                    }
                }
                return false;
            }
            if(!creep.memory.targetId)
            {
                const obj = getTransferObject();
                if(obj)
                {
                    creep.memory.targetId = obj.id;
                }
            }
            if(creep.memory.targetId)
            {
                const store = Game.getObjectById(creep.memory.targetId) as AnyStoreStructure;
                if(store && store.store.getFreeCapacity(RESOURCE_ENERGY)>0)
                {
                    return [ActionArgHelper.actionTransfer(creep,store)];
                }
                creep.memory.targetId=undefined;
            } 
           
        }
        else
        {
         
           
            const getMineAction =(mine:InnerMineEntity,amount:number)=>
            {
                const container = mine.getContainer();
                if(container)
                {
                    if(container.store.getUsedCapacity()>=amount)
                    {
                        return [ActionArgHelper.actionWithdraw(creep,container)]
                    }
                   
                }
                else
                {
                    const pickitem = mine.getPickUpItem();
                    const max_item = _.max(pickitem,e=>e.amount);
                    if(max_item && max_item.amount>=amount)
                    {

                        return [ActionArgHelper.actionPickup(creep,max_item)]
                    }
                    else
                    {
                        // 那就只有移动了
                    }
                }
                return false
            }
            const InnerMines = arg.entity.getEntitys("InnerMineEntity");
         
            for(const e of InnerMines)
            {   
                const acton =  getMineAction(e,creep.store.getFreeCapacity());
                if(acton)
                {
                    
                    return acton
                }
            }
            // 取
        }
        return [ ];
    }
}
